package jun.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import hong.jun.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@DefaultProperties(defaultFallback ="fallbackMethod")//统一托底方法
@EnableHystrixDashboard     //开启HystrixDashboard仪表盘
public class GetuserController {
    //需要配置成Bean
    @Autowired
    private RestTemplate restTemplate ;

    /*  public User fallbackMethod(@PathVariable("id")long id){
        return new User(id,"正在殴打程序员");
    }*/
    //@HystrixCommand(fallbackMethod = "fallbackMethod")   //单个托底方法熔断

    @HystrixCommand
    @GetMapping(value = "/user2/{id}")
    public User touser (@PathVariable("id")long id){
        String url = "http://user1-server/user1/"+id;
        User user = restTemplate.getForObject(url, User.class);
        System.out.println("user2----------"+user);
        return user;
    }


    public User fallbackMethod(){//统一托底方法
        return new User(1,"正在殴打程序员");
    }
}
